/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;

import who.tmap.bo.AttrDef;
import who.tmap.bo.Attrs;
import who.tmap.common.TMapException;
import who.tmap.dao.DataDAO;
import who.tmap.dao.impl.DAOFactory;
import who.tmap.ui.util.DataTableDataModel;
import who.tmap.ui.util.jsorttable.JSortTable;
import who.tmap.ui.util.jsorttable.SortTableModel;

public class JDialogSelectPolygonNameColumn extends JDialog {

	private static final String NO_SELECTION_YET ="-- select polygon name --";
	private boolean cancelled =true;
	private TableModel dataModel =null;
	private List<AttrDef> headers;
	private JDialog thisDialog;
	
	private JPanel jContentPane = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private JPanel jPanelButtons = null;
	private JButton jButtonOK = null;
	private JButton jButtonCancel = null;
	private JPanel jPanelSelect = null;
	//private JLabel jLabel = null;
	private JComboBox jComboBoxColumns = null;
	
	public JDialogSelectPolygonNameColumn(Frame parent, boolean modal, String dbaseFileName) {
		super( parent, modal );
		initialize();
		
		populateUI( dbaseFileName );
	}	
	
	private void populateUI(String dbaseFileName) {
		try {
	        DataDAO attrsDAO =(DataDAO)DAOFactory.getInstance().get( DAOFactory.ATTRS );		        
			Attrs attrs = (Attrs)attrsDAO.loadDbf( dbaseFileName );
			((DataTableDataModel)dataModel).setAttrs( attrs );
			
			getJComboBoxColumns().removeAllItems();
			getJComboBoxColumns().addItem( NO_SELECTION_YET );
			headers =attrs.getHeader();
			for( AttrDef attrDef : headers ) {
				getJComboBoxColumns().addItem( attrDef.getName() );
			}
		} catch (TMapException e) {
			e.printStackTrace();
			
		}  		
	}
	
	public List<AttrDef> getHeader() {
		return headers;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(600, 400);
		this.setTitle("Select polygon name column");
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo(null);
		
		getRootPane().setDefaultButton( getJButtonOK() );
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(5,5,5,5));
			jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
			jContentPane.add(getJPanelButtons(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getJPanelSelect(), java.awt.BorderLayout.NORTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
		    dataModel = new DataTableDataModel();
			
			jTable = new JSortTable( (SortTableModel)dataModel );
		}
		return jTable;
	}

	/**
	 * This method initializes jPanelButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			jPanelButtons = new JPanel();
			jPanelButtons.add(getJButtonOK(), null);
			jPanelButtons.add(getJButtonCancel(), null);
		}
		return jPanelButtons;
	}

	/**
	 * This method initializes jButtonOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");
			jButtonOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if ( getJComboBoxColumns().getItemCount() > 0 && getJComboBoxColumns().getSelectedItem().toString().equals( NO_SELECTION_YET )  ) {
				        JOptionPane.showMessageDialog(thisDialog,
					            "Please select a polygon name column before continuing\nor press Cancel",
					            "TMap error",
					            JOptionPane.WARNING_MESSAGE);   
					} else {
						cancelled =false;
						setVisible( false );
					}
				}
			});
		}
		return jButtonOK;
	}

	/**
	 * This method initializes jButtonCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					cancelled =true;
					setVisible( false );
				}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jPanelSelect	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelSelect() {
		if (jPanelSelect == null) {
			GridLayout gridLayout = new GridLayout(2 ,1);
			//jLabel = new JLabel();
			//jLabel.setText("Please select polygon name column using the combo box below");
			jPanelSelect = new JPanel();
			jPanelSelect.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,5,0));
			jPanelSelect.setLayout(gridLayout);
			//jPanelSelect.add(jLabel);
			jPanelSelect.add(getJComboBoxColumns());
	
		}
		return jPanelSelect;
	}

	/**
	 * This method initializes jComboBoxColumns	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxColumns() {
		if (jComboBoxColumns == null) {
			jComboBoxColumns = new JComboBox();
		}
		return jComboBoxColumns;
	}
	
	public boolean isCancelled() {
		return cancelled;
	}
	
	public String getSelectedColumnName() {
		return (String)getJComboBoxColumns().getSelectedItem();
	}
	
	public int getSelectedColumnIndex() {
		return getJComboBoxColumns().getSelectedIndex();
	}

}
